/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SynonymHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 5, 2009       Raghu Sarraff			       Created
 */
package lwf.handler.english.word;

import java.util.List;
import java.util.Map;

import net.lwf.common.util.CommonUtil;
import net.lwf.handler.util.ImageHtml;
import net.lwf.handler.util.WordUtil;

import lwf.factory.SynonymFactory;

/**
 * Type the class description here
 *  
 */
public class SynonymHandler {

	public static String getAlphabetKeys() {
	 
		List alphabets = SynonymFactory.getInstance().getSynonymAlphabets();
		StringBuffer sbWsKeyBlock = new StringBuffer();

		sbWsKeyBlock.append("<table  width=\"91%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		boolean countdisplay = false;
		int nCounter = 0;
		int nDataCount = 0;
        while(!countdisplay) {
        	sbWsKeyBlock.append("<tr>");
	        	for(int i=0; i<13; i++) {
	        		String value = "";	
	        		sbWsKeyBlock.append("<td class=\"data\" width=\"5%\">");
	        		    String alphabet = "&nbsp;";
	        			if(nDataCount<alphabets.size() && !countdisplay ) {
	        				alphabet = (String) alphabets.get(nDataCount);
	        				String onclick = "displayPickList('" + alphabet + "')";
	        		    	sbWsKeyBlock.append("<a href=\"#\" onclick=\"" + onclick + "\" >");
	        		    	sbWsKeyBlock.append(alphabet);
	        		    	sbWsKeyBlock.append("</a>");
							nDataCount++;
	        		    }
	        		    else {
	        		    	sbWsKeyBlock.append("&nbsp;");
	        		    	countdisplay = true;
	        		    }
	        		sbWsKeyBlock.append("</td>");
	        		sbWsKeyBlock.append("<td width=\"2%\" >");
	        			sbWsKeyBlock.append("&nbsp;");
	        		sbWsKeyBlock.append("</td>");
	        	}
	        	nCounter = nCounter + 13;
        	sbWsKeyBlock.append("</tr>");
        }
        sbWsKeyBlock.append("</table>");      			

        return sbWsKeyBlock.toString();
    }
	
	public static String getPickList(Map mapdata) {
		StringBuffer sbPl = new StringBuffer();
		sbPl.append("<table>");
		    sbPl.append("<tr>");
		        sbPl.append("<td class=\"labelmedium\" >");
		            sbPl.append("Select a word: ");
		        sbPl.append("</td>");
		        sbPl.append("<td>");
		            sbPl.append("<select name=\"synonymkey\" class=\"dropdownbox\" onchange=\"displaySynonyms()\" >");
		                sbPl.append("<option value=\"\" > -- Select a value -- </option>");
		                List data = CommonUtil.getAllKeys(mapdata);
		                if(data != null && data.size() != 0) {
				            for(int i=0; i<data.size(); i++) {
				            	String key = (String) data.get(i);
				            	sbPl.append("<option value=\"" + key + "\"  >");
			            	    sbPl.append(key);
				            	sbPl.append("</option>");
				            }
		                }
		            sbPl.append("</select>");
	            sbPl.append("</td>");
		    sbPl.append("</tr>");
		sbPl.append("</table>");
		return sbPl.toString();
	}
	
	public static String displaySynonyms(String synonymkey, Map mapdata) {
		String[] synonyms = null;
		StringBuffer sbSyn = new StringBuffer();
        if(mapdata.containsKey(synonymkey)) {
        	synonyms = (String[]) mapdata.get(synonymkey);
        }
        if(synonyms != null) {
        	
        	sbSyn.append("<table width=\"100%\" border=\"0\">");
        	String heading = "Following are the synonyms for: " + synonymkey;
        	sbSyn.append("<tr><td class=\"tableheadercenter\" colspan=\"6\">" + heading + "</td></tr>");
    		boolean countdisplay = false;
    		int nCounter = 0;
    		int nDataCount = 0;
            while(!countdisplay) {
            	sbSyn.append("<tr>");
    	        	for(int k=0; k<3; k++) {
    	        		sbSyn.append("<td>");
	        		    String alphabet = "&nbsp;";
	        			if(nDataCount<synonyms.length && !countdisplay ) {
	        				sbSyn.append(WordUtil.getWordData(synonyms[nDataCount], false));
							nDataCount++;
	        		    }
	        		    else {
	        		    	sbSyn.append("&nbsp;");
	        		    	countdisplay = true;
	        		    }
	        			sbSyn.append("</td>");
	        			sbSyn.append("<td>");
	        				sbSyn.append(ImageHtml.getEmptyImage());
	        			sbSyn.append("</td>");
    	        	}
    	        nCounter = nCounter + 3;
    	        sbSyn.append("</tr>");
    	        sbSyn.append("<tr><td colspan=\"6\"><hr></td></tr>");
            }
            
            sbSyn.append("</table>");
        }
        
        return sbSyn.toString();
        
	}
	
} //SynonymHandler

